home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Buttons and Labels and Scrolls (Oh, My!) / HexCalc / HexCalc.cs next >
Encoding:
Text File  |  2001-01-15  |  5.7 KB  |  151 lines

  1. //--------------------------------------
  2. // HexCalc.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class HexCalc: Form
  9. {
  10.      Button btnResult;
  11.      ulong  ulNum       = 0;
  12.      ulong  ulFirstNum  = 0;     
  13.      bool   bNewNumber  = true;
  14.      char   chOperation = '=';
  15.  
  16.      public static void Main()
  17.      {
  18.           Application.Run(new HexCalc());
  19.      }
  20.      public HexCalc()
  21.      {
  22.           Text = "Hex Calc";
  23.           Icon = new Icon(GetType(), "HexCalc.HexCalc.ico");
  24.           FormBorderStyle = FormBorderStyle.FixedSingle;
  25.           MaximizeBox = false;
  26.  
  27.           new CalcButton(this, "D",       'D',  8,  24, 14, 14);
  28.           new CalcButton(this, "A",       'A',  8,  40, 14, 14);
  29.           new CalcButton(this, "7",       '7',  8,  56, 14, 14);
  30.           new CalcButton(this, "4",       '4',  8,  72, 14, 14);
  31.           new CalcButton(this, "1",       '1',  8,  88, 14, 14);
  32.           new CalcButton(this, "0",       '0',  8, 104, 14, 14);
  33.           new CalcButton(this, "E",       'E', 26,  24, 14, 14);
  34.           new CalcButton(this, "B",       'B', 26,  40, 14, 14);
  35.           new CalcButton(this, "8",       '8', 26,  56, 14, 14);
  36.           new CalcButton(this, "5",       '5', 26,  72, 14, 14);
  37.           new CalcButton(this, "2",       '2', 26,  88, 14, 14);
  38.           new CalcButton(this, "Back", '\x08', 26, 104, 32, 14);
  39.           new CalcButton(this, "C",       'C', 44,  40, 14, 14);
  40.           new CalcButton(this, "F",       'F', 44,  24, 14, 14);
  41.           new CalcButton(this, "9",       '9', 44,  56, 14, 14);
  42.           new CalcButton(this, "6",       '6', 44,  72, 14, 14);
  43.           new CalcButton(this, "3",       '3', 44,  88, 14, 14);
  44.           new CalcButton(this, "+",       '+', 62,  24, 14, 14);
  45.           new CalcButton(this, "-",       '-', 62,  40, 14, 14);
  46.           new CalcButton(this, "*",       '*', 62,  56, 14, 14);
  47.           new CalcButton(this, "/",       '/', 62,  72, 14, 14);
  48.           new CalcButton(this, "%",       '%', 62,  88, 14, 14);
  49.           new CalcButton(this, "Equals",  '=', 62, 104, 32, 14);
  50.           new CalcButton(this, "&&",      '&', 80,  24, 14, 14);
  51.           new CalcButton(this, "|",       '|', 80,  40, 14, 14);
  52.           new CalcButton(this, "^",       '^', 80,  56, 14, 14);
  53.           new CalcButton(this, "<",       '<', 80,  72, 14, 14);
  54.           new CalcButton(this, ">",       '>', 80,  88, 14, 14);
  55.  
  56.           btnResult = 
  57.                new CalcButton(this, "0", '\x1B', 8, 4, 86, 14);
  58.  
  59.           foreach (Button btn in Controls)
  60.                btn.Click += new EventHandler(ButtonOnClick);
  61.  
  62.           ClientSize = new Size(102, 126);
  63.           Scale(Font.Height / 8f);
  64.      }
  65.      protected override void OnKeyPress(KeyPressEventArgs kpea)
  66.      {
  67.           char chKey = Char.ToUpper(kpea.KeyChar);
  68.  
  69.           if (chKey == '\x0D')
  70.                chKey = '=';
  71.  
  72.           for (int i = 0; i < Controls.Count; i++)
  73.           {
  74.                CalcButton btn = (CalcButton) Controls[i];
  75.  
  76.                if (chKey == btn.chKey)
  77.                {
  78.                     InvokeOnClick(btn, EventArgs.Empty);
  79.                     break;
  80.                }
  81.           }
  82.      }
  83.      void ButtonOnClick(object obj, EventArgs ea)
  84.      {
  85.           CalcButton btn = (CalcButton) obj;
  86.  
  87.           if (btn.chKey == '\x08')
  88.                ulNum /= 16;
  89.  
  90.           else if (btn.chKey == '\x1B')
  91.                ulNum = 0;
  92.  
  93.           else if (Char.IsLetterOrDigit(btn.chKey))    // Hex digit
  94.           {
  95.                if (bNewNumber)
  96.                {
  97.                     ulFirstNum = ulNum;
  98.                     ulNum = 0;
  99.                     bNewNumber = false;
  100.                }
  101.           
  102.                if (ulNum <= ulong.MaxValue >> 4)
  103.                     ulNum = 16 * ulNum + 
  104.                          (ulong)(btn.chKey -
  105.                               (Char.IsDigit(btn.chKey) ? '0' : 'A' - 10));
  106.           }
  107.           else                                         // Operation
  108.           {
  109.                if(!bNewNumber)
  110.                {
  111.                     switch(chOperation)
  112.                     {
  113.                     case '=': ulNum = ulNum; break;
  114.                     case '+': ulNum = ulFirstNum + ulNum; break;
  115.                     case '-': ulNum = ulFirstNum - ulNum; break;
  116.                     case '*': ulNum = ulFirstNum * ulNum; break;
  117.                     case '&': ulNum = ulFirstNum & ulNum; break;
  118.                     case '|': ulNum = ulFirstNum | ulNum; break;
  119.                     case '^': ulNum = ulFirstNum ^ ulNum; break;
  120.                     case '<': ulNum = ulFirstNum << (int)ulNum; break;
  121.                     case '>': ulNum = ulFirstNum >> (int)ulNum; break;
  122.                     case '/': ulNum = ulNum != 0 ? 
  123.                                         ulFirstNum / ulNum : ulong.MaxValue; 
  124.                               break;
  125.                     case '%': ulNum = ulNum != 0 ? 
  126.                                         ulFirstNum % ulNum : ulong.MaxValue; 
  127.                               break;
  128.                     default:  ulNum = 0; break;
  129.                     }
  130.                }
  131.                bNewNumber = true;
  132.                chOperation = btn.chKey;
  133.           }
  134.           btnResult.Text = String.Format("{0:X}", ulNum);
  135.      }
  136. }
  137. class CalcButton: Button
  138. {
  139.      public char chKey;
  140.  
  141.      public CalcButton(Control parent, string str, char chkey, 
  142.                        int x, int y, int cx, int cy)
  143.      {
  144.           Parent   = parent;
  145.           Text     = str;
  146.           chKey    = chkey;
  147.           Location = new Point(x, y);
  148.           Size     = new Size(cx, cy);
  149.           SetStyle(ControlStyles.Selectable, false);
  150.      }
  151. }